package com.meis.widget.horizontal;

import com.meis.widget.utils.DensityUtil;
import com.meis.widget.utils.PixelUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.agp.utils.TextTool;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.ImageInfo;
import ohos.media.image.common.Size;
import org.jetbrains.annotations.Nullable;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by wenshi on 2018/12/12.
 * Description 垂直排版的文本控件
 */
public class VerticalTextView extends Text implements Component.DrawTask {

    protected Paint mPaint;

    // 字符间距
    private int mCharSpacing;
    // 默认文本
    private String mDefaultText = "左滑看更多";

    // 背景阴影画笔
    private Paint mShadowPaint;
    private Path mShadowPath;

    private float mShadowOffset = 0;
    private int mErrorDistance;
    private boolean mIsDrawShadow = true;

    public VerticalTextView(Context context) {
        this(context, null);
    }

    public VerticalTextView(Context context, @Nullable AttrSet attrs) {
        this(context, attrs, "");
    }

    public VerticalTextView(Context context, @Nullable AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);

        PixelUtil.initContext(context);

        //图标放大
        Element[] elements = getAroundElements();
        for (Element element : elements) {
            if(element instanceof PixelMapElement){
                PixelMap pixelMap = ((PixelMapElement) element).getPixelMap();
                if(pixelMap != null){
                    ImageInfo info = pixelMap.getImageInfo();
                    PixelMap.InitializationOptions opts = new PixelMap.InitializationOptions();
                    opts.size = new Size(info.size.width + 12, info.size.height + 12);
                    opts.pixelFormat = info.pixelFormat;
                    PixelMap pixel = PixelMap.create(pixelMap, opts);
                    setAroundElements(new PixelMapElement(pixel), null, null, null);
                    break;
                }
            }
        }

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setColor(Color.BLACK);

        //　默认为4dp
        mCharSpacing = DensityUtil.dp2px(context, 4);
        mErrorDistance = DensityUtil.dp2px(context, 8);

        mShadowPaint = new Paint();
        mShadowPaint.setColor(new Color(Color.getIntColor("#4F000000")));
        mShadowPaint.setAntiAlias(true);
        mShadowPaint.setStyle(Paint.Style.FILL_STYLE);
        mShadowPaint.setStrokeWidth(1);

        mShadowPath = new Path();

        addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        mPaint.setTextSize(getTextSize());
        String text = mDefaultText;
        if (text != null && !text.trim().equals("")) {
            Rect bounds = mPaint.getTextBounds(text);
            int startX = bounds.getWidth() - getPaddingLeft() - getPaddingRight() + getAroundElementsPadding() + 12;
            int startY = getHeight() / 2 - bounds.getWidth() / 2;

            int cHeight = (bounds.bottom - bounds.top + mCharSpacing);
            for (int i = 0; i < text.length(); i++) {
                String c = String.valueOf(text.charAt(i));
                canvas.drawText(mPaint, c, startX, startY + i * cHeight);
            }
        }

        // 绘制贝塞尔阴影
        if (mIsDrawShadow) {
            mShadowPath.reset();
            mShadowPath.moveTo(getWidth(), getHeight() / 4);
            mShadowPath.quadTo(mShadowOffset, getHeight() / 2, getWidth(), getHeight() / 4 * 3);
            canvas.drawPath(mShadowPath, mShadowPaint);
        }
    }

    @Override
    public void setText(String text) {
        mDefaultText = text;
        super.setText("");
    }

    public void setVerticalText(String text) {
        if (TextTool.isNullOrEmpty(text)) return;
        mDefaultText = text;
        invalidate();
    }

    public boolean getIsDrawShadow() {
        return mIsDrawShadow;
    }

    public void setDrawShadow(boolean drawShadow) {
        mIsDrawShadow = drawShadow;
    }

    public void setOffset(float offset, float maxOffset) {
        this.mShadowOffset = offset;
        float dis = maxOffset / 2 - mErrorDistance;
        if (mShadowOffset >= dis) {
            mShadowOffset = dis;
        } else {
            mShadowOffset = dis + (offset - dis) / 2;
        }
        invalidate();
    }
}
